Hanye官网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[id].vue 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <template>
  2. <div>
  3. <div class="w-full h-[55px] sm:h-[72px]"></div>
  4. <ErrorBoundary :error="error">
  5. <div v-if="isLoading" class="flex justify-center py-12">
  6. <!-- 加载中 -->
  7. <div
  8. class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"
  9. ></div>
  10. </div>
  11. <div v-else>
  12. <!-- 面包屑导航 -->
  13. <div class="max-w-full mb-6 xl:px-2 lg:px-2 md:px-4 px-4 mt-6">
  14. <div class="max-w-screen-2xl mx-auto">
  15. <nuxt-link
  16. to="/"
  17. class="justify-start text-white/60 text-base font-normal"
  18. >ホーム</nuxt-link
  19. >
  20. <span class="text-white/60 text-base font-normal px-2"> / </span>
  21. <nuxt-link to="/products" class="text-white/60 text-base font-normal"
  22. >製品一覧</nuxt-link
  23. >
  24. <span class="text-white/60 text-base font-normal px-2"> / </span>
  25. <span class="text-white text-base font-normal">{{ product?.name }}</span>
  26. </div>
  27. </div>
  28. <!-- 产品详情内容 -->
  29. <div class="max-w-full mb-12 md:mb-20 lg:mb-32 xl:px-2 lg:px-2 md:px-4 px-4">
  30. <div class="max-w-screen-2xl mx-auto">
  31. <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 lg:gap-16">
  32. <!-- 左侧产品图片 -->
  33. <div class="flex flex-col gap-6">
  34. <!-- 主图展示 -->
  35. <div
  36. class="bg-zinc-900 rounded-lg p-8 relative overflow-hidden group aspect-square"
  37. >
  38. <!-- 加载状态 -->
  39. <div v-if="isImageLoading" class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 z-10">
  40. <div class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"></div>
  41. </div>
  42. <!-- 主图容器 -->
  43. <div class="relative w-full h-full">
  44. <!-- 当前图片 -->
  45. <img
  46. :src="currentImage"
  47. :alt="product?.name"
  48. class="absolute inset-0 w-full h-full object-contain rounded-lg transition-all duration-500"
  49. :class="{
  50. 'opacity-0': isImageLoading,
  51. 'opacity-100': !isImageLoading
  52. }"
  53. @load="handleImageLoad"
  54. @error="handleImageError"
  55. />
  56. <!-- 预加载图片 -->
  57. <img
  58. v-if="preloadImage"
  59. :src="preloadImage"
  60. class="absolute inset-0 w-full h-full object-contain rounded-lg opacity-0"
  61. @load="handlePreloadComplete"
  62. />
  63. </div>
  64. <!-- 错误提示 -->
  65. <div
  66. v-if="imageError"
  67. class="absolute inset-0 flex items-center justify-center bg-red-900/50 z-20"
  68. >
  69. <div class="flex flex-col items-center gap-2">
  70. <span class="text-white">画像の読み込みに失敗しました</span>
  71. <button
  72. @click.stop="retryLoadImage"
  73. class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors duration-300"
  74. >
  75. 再試行
  76. </button>
  77. </div>
  78. </div>
  79. </div>
  80. <!-- 缩略图列表 -->
  81. <div class="flex gap-4 overflow-x-auto pb-2 scrollbar-hide">
  82. <div
  83. v-for="(image, index) in [product?.image, ...(product?.gallery || [])]"
  84. :key="index"
  85. @click="changeImage(image)"
  86. class="flex-shrink-0 w-20 h-20 cursor-pointer rounded-lg transition-all duration-300 relative group aspect-square p-0.5"
  87. :class="{
  88. 'bg-gradient-to-r from-blue-500 to-blue-600': currentImage === image,
  89. 'hover:bg-gradient-to-r hover:from-blue-500/50 hover:to-blue-600/50': currentImage !== image,
  90. 'opacity-50': isThumbnailLoading[index] || thumbnailErrors[index]
  91. }"
  92. >
  93. <!-- 缩略图加载状态 -->
  94. <div v-if="isThumbnailLoading[index]" class="absolute inset-0 flex items-center justify-center bg-zinc-800 rounded-lg">
  95. <div class="animate-spin h-4 w-4 border-2 border-blue-500 rounded-full border-t-transparent"></div>
  96. </div>
  97. <!-- 缩略图遮罩 -->
  98. <div
  99. class="absolute inset-0 bg-black/0 transition-all duration-300 rounded-lg"
  100. :class="{
  101. 'bg-black/30': currentImage === image,
  102. 'group-hover:bg-black/20': currentImage !== image
  103. }"
  104. ></div>
  105. <img
  106. :src="image"
  107. :alt="`${product?.name} - 画像 ${index + 1}`"
  108. class="w-full h-full object-cover transition-all duration-300 rounded-lg"
  109. :class="{
  110. 'opacity-0': isThumbnailLoading[index],
  111. 'opacity-100': !isThumbnailLoading[index],
  112. 'group-hover:scale-110': currentImage !== image
  113. }"
  114. @load="handleThumbnailLoad(index)"
  115. @error="handleThumbnailError(index)"
  116. />
  117. <!-- 选中标记 -->
  118. <div
  119. v-if="currentImage === image"
  120. class="absolute top-1 right-1 w-4 h-4 bg-blue-500 rounded-full flex items-center justify-center"
  121. >
  122. <div class="w-2 h-2 bg-white rounded-full"></div>
  123. </div>
  124. <!-- 缩略图错误提示 -->
  125. <div
  126. v-if="thumbnailErrors[index]"
  127. class="absolute inset-0 flex items-center justify-center bg-red-900/50 rounded-lg"
  128. >
  129. <div class="flex flex-col items-center gap-1">
  130. <span class="text-white text-xs">エラー</span>
  131. <button
  132. @click.stop="retryLoadThumbnail(index)"
  133. class="px-2 py-1 bg-blue-500 text-white text-xs rounded hover:bg-blue-600 transition-colors duration-300"
  134. >
  135. 再試行
  136. </button>
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. </div>
  142. <!-- 右侧产品信息 -->
  143. <div class="flex flex-col gap-8">
  144. <!-- 产品名称 -->
  145. <div class="bg-zinc-900 rounded-lg p-6">
  146. <h1 class="text-white text-3xl font-medium mb-4">
  147. {{ product?.name }}
  148. </h1>
  149. <div class="text-stone-400 text-lg leading-relaxed">
  150. {{ product?.description }}
  151. </div>
  152. </div>
  153. <!-- 产品参数 -->
  154. <div class="bg-zinc-900 rounded-lg p-6">
  155. <h2 class="text-white text-xl font-medium mb-6">製品仕様</h2>
  156. <div class="grid grid-cols-1 gap-4">
  157. <div class="flex justify-between items-center py-2 border-b border-zinc-800">
  158. <span class="text-stone-400">カテゴリー</span>
  159. <span class="text-white font-medium">{{ product?.category }}</span>
  160. </div>
  161. <div class="flex justify-between items-center py-2 border-b border-zinc-800">
  162. <span class="text-stone-400">用途</span>
  163. <span class="text-white font-medium">{{ product?.usage }}</span>
  164. </div>
  165. <div class="flex justify-between items-center py-2">
  166. <span class="text-stone-400">容量</span>
  167. <span class="text-white font-medium">{{ product?.capacities.join(" / ") }}</span>
  168. </div>
  169. </div>
  170. </div>
  171. <!-- 产品描述 -->
  172. <div class="bg-zinc-900 rounded-lg p-6">
  173. <h2 class="text-white text-xl font-medium mb-6">製品説明</h2>
  174. <div class="text-stone-400 leading-relaxed space-y-4">
  175. <p>{{ product?.description }}</p>
  176. </div>
  177. </div>
  178. </div>
  179. </div>
  180. </div>
  181. </div>
  182. </div>
  183. </ErrorBoundary>
  184. </div>
  185. </template>
  186. <script setup lang="ts">
  187. /**
  188. * 产品详情页面
  189. * 展示产品主图、参数和描述
  190. */
  191. import { useErrorHandler } from "~/composables/useErrorHandler";
  192. // 产品接口定义
  193. interface Product {
  194. id: number;
  195. name: string;
  196. category: string;
  197. usage: string;
  198. capacities: string[];
  199. image: string;
  200. description: string;
  201. gallery?: string[]; // 添加相册图片数组
  202. }
  203. const { error, isLoading, wrapAsync } = useErrorHandler();
  204. const route = useRoute();
  205. const product = ref<Product | null>(null);
  206. const currentImage = ref<string>("");
  207. const isImageLoading = ref(true);
  208. const isThumbnailLoading = ref<boolean[]>([]);
  209. const imageError = ref(false);
  210. const thumbnailErrors = ref<boolean[]>([]);
  211. const preloadImage = ref<string | null>(null);
  212. /**
  213. * 加载产品详情
  214. */
  215. async function loadProduct() {
  216. await wrapAsync(async () => {
  217. const id = route.params.id;
  218. const response = await $fetch<Product>(`/api/products/${id}`);
  219. product.value = response;
  220. currentImage.value = response.image;
  221. return response;
  222. });
  223. }
  224. /**
  225. * 预加载下一张图片
  226. */
  227. function preloadNextImage(image: string) {
  228. preloadImage.value = image;
  229. }
  230. /**
  231. * 处理预加载完成
  232. */
  233. function handlePreloadComplete() {
  234. preloadImage.value = null;
  235. }
  236. /**
  237. * 处理图片加载完成
  238. */
  239. function handleImageLoad() {
  240. isImageLoading.value = false;
  241. imageError.value = false;
  242. }
  243. /**
  244. * 处理图片加载错误
  245. */
  246. function handleImageError() {
  247. isImageLoading.value = false;
  248. imageError.value = true;
  249. }
  250. /**
  251. * 重试加载图片
  252. */
  253. function retryLoadImage() {
  254. isImageLoading.value = true;
  255. imageError.value = false;
  256. // 强制重新加载图片
  257. const img = new Image();
  258. img.src = currentImage.value;
  259. img.onload = () => {
  260. handleImageLoad();
  261. };
  262. img.onerror = () => {
  263. handleImageError();
  264. };
  265. }
  266. /**
  267. * 重试加载缩略图
  268. */
  269. function retryLoadThumbnail(index: number) {
  270. isThumbnailLoading.value[index] = true;
  271. thumbnailErrors.value[index] = false;
  272. // 强制重新加载缩略图
  273. const img = new Image();
  274. const images = [product.value?.image, ...(product.value?.gallery || [])];
  275. img.src = images[index] || '';
  276. img.onload = () => {
  277. handleThumbnailLoad(index);
  278. };
  279. img.onerror = () => {
  280. handleThumbnailError(index);
  281. };
  282. }
  283. /**
  284. * 处理缩略图加载完成
  285. */
  286. function handleThumbnailLoad(index: number) {
  287. isThumbnailLoading.value[index] = false;
  288. thumbnailErrors.value[index] = false;
  289. }
  290. /**
  291. * 处理缩略图加载错误
  292. */
  293. function handleThumbnailError(index: number) {
  294. isThumbnailLoading.value[index] = false;
  295. thumbnailErrors.value[index] = true;
  296. }
  297. /**
  298. * 切换图片
  299. */
  300. function changeImage(image: string | undefined) {
  301. if (image && image !== currentImage.value) {
  302. isImageLoading.value = true;
  303. imageError.value = false;
  304. preloadNextImage(image);
  305. currentImage.value = image;
  306. }
  307. }
  308. // 页面加载时获取产品数据
  309. onMounted(() => {
  310. loadProduct();
  311. // 初始化缩略图加载状态数组
  312. isThumbnailLoading.value = Array(4).fill(true);
  313. thumbnailErrors.value = Array(4).fill(false);
  314. });
  315. // SEO优化
  316. useHead(() => ({
  317. title: `${product.value?.name || "产品详情"} - Hanye`,
  318. meta: [
  319. {
  320. name: "description",
  321. content: product.value?.description || "产品详情页面",
  322. },
  323. ],
  324. }));
  325. </script>
  326. <style scoped>
  327. /* 隐藏滚动条但保持滚动功能 */
  328. .scrollbar-hide {
  329. -ms-overflow-style: none; /* IE and Edge */
  330. scrollbar-width: none; /* Firefox */
  331. }
  332. .scrollbar-hide::-webkit-scrollbar {
  333. display: none; /* Chrome, Safari and Opera */
  334. }
  335. /* 图片过渡动画 */
  336. .main-image {
  337. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  338. }
  339. /* 缩略图悬停效果 */
  340. .thumbnail-item {
  341. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  342. }
  343. .thumbnail-item:hover {
  344. transform: translateY(-2px);
  345. }
  346. /* 缩略图选中效果 */
  347. .thumbnail-item.selected {
  348. transform: scale(1.05);
  349. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  350. }
  351. /* 产品信息卡片效果 */
  352. .info-card {
  353. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  354. }
  355. .info-card:hover {
  356. transform: translateY(-2px);
  357. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  358. }
  359. </style>